#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <locale.h>

#define QUEUE_MAX_SIZE 100

typedef struct Queue
{
    int elements[QUEUE_MAX_SIZE];
    int front;
    int rear;
    int size;
} Queue;

int init_queue(Queue **q)
{
    *q = (Queue *)malloc(sizeof(Queue));
    if (*q == NULL)
    {
        return 1;
    }
    (*q)->front = 0;
    (*q)->rear = -1;
    (*q)->size = 0;
    return 0;
}

int enqueue(Queue *q, int value)
{
    if (q->size >= QUEUE_MAX_SIZE)
    {
        return 1;
    }
    q->rear = (q->rear + 1) % QUEUE_MAX_SIZE;
    q->elements[q->rear] = value;
    q->size++;
    return 0;
}

int dequeue(Queue *q, int *value)
{
    if (q->size <= 0)
    {
        return 1;
    }
    *value = q->elements[q->front];
    q->front = (q->front + 1) % QUEUE_MAX_SIZE;
    q->size--;
    return 0;
}

int first(Queue *q, int *value)
{
    if (q->size <= 0)
    {
        return 1;
    }
    *value = q->elements[q->front];
    return 0;
}

int is_empty(Queue *q)
{
    return(q->size == 0) ? 0 : 1;
}

int size(Queue *q)
{
    return q->size;
}

int main()
{
    setlocale(LC_ALL, "chs");
    Queue *q;
    int value;

    if (init_queue(&q) != 0)
    {
        printf("队列初始化失败\n");
        return 1;
    }
    printf("队列初始化成功\n");

    for (int i = 1; i <= 5; i++)
    {
        if (enqueue(q, i))
        {
            printf("入队失败：%d\n", i);
        }else
        {
            printf("入队成功：%d\n", i);
        }
    }

    printf("当前队列大小：%d\n", size(q));

    if (first(q, &value))
    {
        printf("获取队首元素失败\n");
    }else
    {
        printf("队首元素：%d\n", value);
    }

    while (is_empty(q))
    {
        if (dequeue(q, &value))
        {
            printf("出队失败\n");
        }else
        {
            printf("出队元素：%d\n", value);
        }
    }

    printf("队列是否为空：%s\n", is_empty(q) == 0 ? "yes" : "no");

    free(q);

    return 0;
}